Ext.Loader.setConfig({
    enabled: true
});
Ext.Loader.setPath('Ext.ux', rootPath + '/res/extjs/ux/');
Ext.require(['Ext.data.*', 'Ext.grid.*', 'Ext.util.*',
    'Ext.form.field.ComboBox', 'Ext.form.FieldSet',
    'Ext.tip.QuickTipManager', 'Ext.ux.data.PagingMemoryProxy'

]);

var data, store, columns, queryGrid, pager;
Ext.onReady(function () {
    Ext.QuickTips.init();
    Ext.EventManager.onWindowResize(function () {
        queryGrid.getView().refresh();
    });
    initGrid();
    initCombo();
    initBtnFunc();
});

function initBtnFunc() {
    $(".btn-search").click(function () {
        store.proxy.extraParams = {
            bikeStatusCode: $("#bikeStatusCode").val(),
            bikeNum: $("#bikeNum").val()
        };
        store.load();
    });

    $(".btn-reset").click(
        function () {
            $("#bikeStatusCode").val(""), $("#bikeNum").val("")
        });
}

/* init combo */
function initCombo() {
    var store = Ext.create('Ext.data.Store', {
        fields: ["bikeStatusCode", "bikeStatusValue"],
        data: [{
            "bikeStatusCode": 1,
            "bikeStatusValue": "正常"
        },
            {
                "bikeStatusCode": 2,
                "bikeStatusValue": "报修"
            },
            {
                "bikeStatusCode": 3,
                "bikeStatusValue": "已报废"
            }
        ]
    });
    var simpleCombo = Ext.create('Ext.form.field.ComboBox', {
        renderTo: 'bikeStatusCombo',
        displayField: 'bikeStatusValue',
        valueField: 'bikeStatusCode',
        width: 220,
        labelWidth: 130,
        store: store,
        typeAhead: true,
        listeners: {
            select: function (value) {
                $("#bikeStatusCode").val(this.getValue());
            },
            // 渲染之后的回调
            render: function () {
                this.setValue("所有");
            }
        }
    });
}

/* init grid */
function initGrid() {
    // create the data store
    store = Ext.create('Ext.data.Store', {
        fields: [{
            name: 'bikeNum',
            type: 'auto'
        }, {
            name: 'storageDate',
            type: 'auto'
        }, {
            name: 'status',
            type: 'auto'
        }, {
            name: 'useCount',
            type: 'auto'
        }, {
            name: 'lastUseTime',
            type: 'auto'
        }, {
            name: 'remark',
            type: 'auto'
        }],
        remoteSort: true,
        pageSize: 10,
        proxy: {
            type: 'ajax',
            url: rootPath + '/bike-info/list-bikes',
            reader: {
                type: 'json',
                root: 'rows',
                totalProperty: 'total'
            }
        }
    });
    // width确定的宽度
    //

    columns = [{
        text: '车牌号',
        sortable: false,
        dataIndex: 'bikeNum',
        align: 'center',
        tdCls: 'ver-mid'
    }, {
        text: '入库时间',
        width: 90,
        sortable: true,
        dataIndex: 'storageDate',
        tdCls: 'ver-mid'
    }, {
        text: '当前自行车状态',
        width: 90,
        sortable: true,
        dataIndex: 'status',
        renderer: function (value) {
            var str = "";
            if (value == 1) {
                str = "正常";
            } else if (value == 2) {
                str = "报修";
            } else if (value == 3) {
                str = "报废";
            }
            return str;
        },
        align: 'center'
    }, {
        text: '使用次数',
        width: 90,
        sortable: true,
        dataIndex: 'useCount',
        tdCls: 'ver-mid'
    }, {
        text: '最后一次被使用时间',
        width: 90,
        sortable: true,
        dataIndex: 'lastUseTime',
        tdCls: 'ver-mid'
    }, {
        text: '备注',
        width: 90,
        sortable: true,
        dataIndex: 'remark',
        tdCls: 'ver-mid'
    }, {
        text: '操作',
        menuDisabled: true,
        sortable: false,
        flex: 1,
        renderer: buttonRender,
        align: 'center',
        tdCls: 'ver-mid'
    }];
    // 新增
    var dockedItems = [{
        xtype: 'toolbar',
        items: [{
            text: '',
            tooltip: '新增',
            minWidth: 30,
            minHeight: 30,
            iconCls: 'new-ico',
            listeners: {
                click: {
                    element: 'el',
                    fn: function () {
                        window.location.href = "addItem.jsp";
                    }
                }
            }
        }]
    }];
    // 多选
    var selModel = Ext.create('Ext.selection.CheckboxModel', {
        listeners: {
            selectionchange: function (sm, selections) {
            }
        }
    });
    // pager
    pager = Ext.create('Ext.PagingToolbar', {
        store: store,
        displayInfo: true,
        displayMsg: '显示第 {0} 条到 {1} 条记录,一共 {2} 条'
    });
    // create the Grid
    queryGrid = Ext.create('Ext.grid.Panel', {
        store: store,
        stateful: true,
        collapsible: false,
        multiSelect: true,
        stateId: 'stateGrid',
        columns: columns,
        // height: 300,
        selModel: selModel,
        dockedItems: dockedItems,
        renderTo: 'queryGrid',
        bbar: pager,
        viewConfig: {
            stripeRows: true,
            enableTextSelection: true,
            deferRowRender: false,
            forceFit: true,
            emptyText: "<font class='emptyText'>没有符合条件的记录</font>",
            autoScroll: true,
            scrollOffset: -10
        }
    });
    store.load();

}

/*
 * 操作按钮
 */
function buttonRender(value, meta, record, rowIndex, colIndex, store) {
    var returnValue = "";
    var itemApprState = record.data.status;
    var bikeNum = record.data.bikeNum;// 获取主键值
    // alert(advState);
    if (itemApprState == '1') {// 正常
        returnValue += '<a href="' + rootPath
            + '/bike-info/update-bike-status?bikeStatus=2&bikeNum=' + bikeNum
            + '">报修&nbsp</a>' + '<a href="' + rootPath
            + '/bike-info/update-bike-status?bikeStatus=3&bikeNum=' + bikeNum
            + '">报废&nbsp</a>'
    } else if (itemApprState == '2') {// 报修
        returnValue += '<a href="' + rootPath
            + '/bike-info/update-bike-status?bikeStatus=1&bikeNum=' + bikeNum
            + '">已修&nbsp</a>' + '<a href="' + rootPath
            + '/bike-info/update-bike-status?bikeStatus=3&bikeNum=' + bikeNum
            + '">报废&nbsp</a>'
    } else if (itemApprState == '3') {// 已报废
        returnValue += '<a href="' + rootPath
            + '/bike-info/delete-bike?bikeNum=' + bikeNum
            + '">删除&nbsp</a>'
    }
    return returnValue;
}
